package com.geek.novel.services.manager

import android.text.TextUtils
import com.geek.novel.common.Constant
import com.geek.novel.entity.SysUser
import com.geek.novel.listener.ApiResultListener
import com.geek.novel.network.RslHandler
import com.geek.novel.repository.WebRepository
import com.geek.novel.services.AuthService
import com.geek.novel.utils.LogCatUtil
import com.geek.novel.utils.SPUtil
import com.geek.novel.utils.SecurityUtil
import com.geek.novel.utils.ToastUtils
import com.geek.novel.vo.ApiErrorResult
import com.geek.novel.vo.ApiResult

/**用户信息管理器*/
class AuthManager: RslHandler {

    private var authService: AuthService = AuthService(this)

    private var apiResultListener: ApiResultListener? = null
    fun setOnApiResultListener(listener: ApiResultListener?){
        this.apiResultListener = listener
    }

    override fun onError(code: Int, err: ApiErrorResult) {
        if(this.apiResultListener != null){
            this.apiResultListener!!.handleError(code, err)
        }
    }
    override fun onSuccess(result: ApiResult, code: Int) {
        when(code){
            //登录
            Constant.HTTP_KEY_LOGIN->this.handleLogin(result)
            Constant.HTTP_KEY_REGIST->this.handleLogin(result)
            //获取用户信息
            Constant.HTTP_KEY_GET_USERINFO->{
                val sysUser = result.getData() as SysUser
                SecurityUtil.setPrincipal(sysUser)
            }
            //更新用户信息
            Constant.HTTP_KEY_UPDATE_USERINFO->{
                val sysUser = result.getData() as SysUser
                SecurityUtil.setPrincipal(sysUser)
            }
        }
        if(this.apiResultListener != null){
            this.apiResultListener!!.handleResult(result, code)
        }
    }

    /**处理登录结果*/
    private fun handleLogin(result: ApiResult) {
        val logonResult = result.getData() as AuthService.LoginResult
        LogCatUtil.e("Test", "${logonResult.token}")
        //存放token，获取用户信息
        val token = logonResult.token
        if(!TextUtils.isEmpty(token)){
            SPUtil.put(Constant.SP_TOKEN, token!!, SPUtil.usrInfo)
            WebRepository.token = token
            //获取用户信息
            this.getUserInfo()
        }else{
            ToastUtils.toast("登录失败！获取token为空！")
        }
    }

    override fun logout(key: Int) {
        if(this.apiResultListener != null){
            this.apiResultListener!!.handleLogout(key)
        }
    }

    /**
     * 登录接口
     */
    fun login(username: String, password: String){
        authService.login(username, password)
    }

    /**
     * 获取用户信息接口
     */
    fun getUserInfo(){
        authService.getUserInfo()
    }

    /**
     * 注册接口
     */
    fun regist(user: SysUser){
        authService.regist(user)
    }

    /**
     * 获取用户信息接口
     */
    fun updateUserInfo(user: SysUser){
        authService.updateUserInfo(user)
    }

    /**
     * 修改密码接口
     */
    fun modifyPassword(user: SysUser, password: String){
        authService.modifyPassword(user, password)
    }

    /**
     * 发送忘记密码的验证码
     */
    fun sendForgetPassValidateCode(target: String){
        authService.sendForgetPassValidateCode(target)
    }

    /**
     * 忘记密码-修改密码
     */
    fun forgetPassword(target: String, code: String, password: String, passwordAgain: String){
        authService.forgetPassword(target, "mail", code, password, passwordAgain)
    }

}